$(function () {
    layui.config({
        base: './../../../static/lib/dist/'
    });

    layui.use([ 'laydate', 'form', 'notice', 'table', 'selectPlus', 'element'],function () {
        var form = layui.form;
        var notice = layui.notice;
        var table = layui.table;
        var laydate = layui.laydate;
        var selectPlus = layui.selectPlus;
        var element = layui.element;


        //查询菜单信息
        function inittable_func() {
            let initjson = {
                elem: '#funclist',
                url: 'http://127.0.0.1:8098/amc/sys/base/menu/func/page',
                params: {
                    menuId: layui.data('menu').func.id
                },
                headers: {'authorization': layui.data('user').token},
                id: 'func_tab',
                cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'funcName', title: '功能名称', edit: 'text'}
                    ,{field:'funcMark', title: '功能标示'}
                    ,{fixed: 'right', title:'操作', toolbar: '#row_bar_func', width:150}
                ]]
            }
            tableinit(table, initjson)
        }

        inittable_func()
        $('#menuName').val(layui.data('menu').func.menuName)

        function tableReload_func(){
            table.reload('func_tab', {
                where: {

                } //设定异步数据接口的额外参数
            });
        }


        //监听行工具事件
        table.on('tool(funcfilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认要删除吗', function(index){
                    deleteFunc(data.id).then(value => {
                        if(value.code == 200){
                            layer.msg('删除成功', {icon: 1});
                            tableReload_func()
                        }else{
                            layer.msg('删除失败，'+value.msg)
                        }
                    }).catch(reason => {
                        layer.msg('删除失败，服务器未连接')
                    })
                });
            }
        });

        form.on('submit(saveFunc)', function (data) {
            let funcInfo = data.field
            funcInfo.menuId = layui.data('menu').func.id
            saveFunc(funcInfo).then(value => {
                if(value.code == 200){
                    layer.msg('保存成功', {icon: 6});
                    tableReload_func()
                }else{
                    notice.error('保存失败，'+value.msg)
                }
            }).catch(reason => {
                notice.error('保存失败，服务器未连接')
            })
        })

        //监听单元格编辑
        table.on('edit(funcfilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            data[field] = value
            updateFunc(data).then(value => {
                if(value.code == 200){
                    layer.msg('修改成功', {icon: 1});
                    tableReload_func()
                }else{
                    layer.msg('修改失败，'+value.msg)
                }
            }).catch(reason => {
                layer.msg('服务器未连结')
            })
        });

    })
})